import {
  NInput,
  NInputNumber,
  NSelect,
  FormRules,
  SelectProps,
  UploadProps,
  NDatePicker,
  NTimePicker,
  DatePickerProps,
  TimePickerProps,
  NRate,
  NSlider,
  NSwitch,
  NColorPicker,
  NTreeSelect,
  NCascader,
  CascaderProps,
  TreeSelectProps
} from 'naive-ui'
import { BasicFormItemPros, ComponentType, PropsForm } from '../types'

export function getComponent(type: ComponentType) {
  switch (type) {
    case 'NInput':
      return NInput
    case 'NInputNumber':
      return NInputNumber
    case 'NSelect':
      return NSelect
    case 'NDatePicker':
      return NDatePicker
    case 'NTimePicker':
      return NTimePicker
    case 'NRate':
      return NRate
    case 'NSlider':
      return NSlider
    case 'NSwitch':
      return NSwitch
    case 'NColorPicker':
      return NColorPicker
    case 'NTreeSelect':
      return NTreeSelect
    case 'NCascader':
      return NCascader
    default:
      return null
  }
}

export function getDefaultPlaceHolder(type: ComponentType, label: string) {
  switch (type) {
    case 'NSelect':
    case 'NCascader':
    case 'NDatePicker':
    case 'NTimePicker':
    case 'NCheckbox':
    case 'NCheckboxGroup':
    case 'NRadio':
    case 'NRadioGroup':
    case 'NTransfer':
    case 'NSlider':
    case 'NSwitch':
      return `请选择${label}`
    case 'NUpload':
      return `请上传${label}`
    default:
      return `请输入${label}`
  }
}

export function getValidateTyep(item: BasicFormItemPros) {
  if (item.component === 'NSelect' && (item.componentProps as SelectProps).multiple) return 'array'
  if (item.component === 'NUpload' && (item.componentProps as UploadProps).multiple) return 'array'
  if (item.component === 'NCascader' && (item.componentProps as CascaderProps).multiple)
    return 'array'
  if (item.component === 'NTreeSelect' && (item.componentProps as TreeSelectProps).multiple)
    return 'array'

  const arrayComponents = ['NCheckboxGroup', 'NTransfer']
  if (arrayComponents.includes(item.component)) return 'array'

  const numberComponents = ['NInputNumber', 'NRate', 'NSlider']
  if (numberComponents.includes(item.component)) return 'number'

  const booleanComponents = ['NSwitch']
  if (booleanComponents.includes(item.component)) return 'boolean'

  if (item.component === 'NDatePicker') {
    const cur = item.componentProps as DatePickerProps
    const range = ['daterange', 'datetimerange', 'monthrange', 'quarterrange', 'yearrange']
    if (range.includes(cur.type as string)) {
      return 'array'
    }
    if (cur.valueFormat) return 'string'
    return 'number'
  }

  if (item.component === 'NTimePicker') {
    const cur = item.componentProps as TimePickerProps
    if (cur.valueFormat) return 'string'
    return 'number'
  }
  return undefined
}

export function getFormRules(list: PropsForm) {
  const requiredList = list.filter((k) => k.required)
  const obj: FormRules = {}
  requiredList.forEach((k) => {
    obj[k.key] = {
      required: true,
      type: getValidateTyep(k) as any,
      message: !k.validator ? getDefaultPlaceHolder(k.component, k.label) : undefined,
      trigger: ['blur', 'change', 'input'],
      validator: k.validator,
      key: k.key
    }
  })
  return obj
}
